class Compiler {
    constructor(vm){
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }
    // 编译模板，处理文本节点和元素节点
    compile(el){
        let childNodes = el.childNodes
        Array.from(childNodes).forEach(node => {
            if(this.isTextNode(node)){
                this.compileText(node)
            }else if(this.isElementNode(node)){
                this.compileElement(node)
            }
            // 如果有子节点，需要递归调用子节点
            if(node.childNodes && node.childNodes.length){
                this.compile(node)
            }
        })
    }
    // 编译元素节点，处理指令
    compileElement(node) {
        // v-text和v-html
        // let that = this
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            let eventName = ''
            if(this.isDirective(attrName)){
                // 如果是包含on:或者@则应该调用on
                if(attrName.indexOf('v-on:')!=-1){
                    eventName = attrName.substr(5)
                    attrName = 'on' 
                }else if(attrName.indexOf('@')!=-1) {
                    eventName = attrName.substr(1)
                    attrName = 'on' 
                }else{
                    attrName = attrName.substr(2)
                }
                let key = attr.value
                this.update(node,key,attrName,eventName)
               
            }
        })
    }
    update(node,key,attrName,eventName){
        let updateFn = this[attrName+'Update']
        updateFn && updateFn.call(this,node,this.vm[key],key,eventName)
    }
    textUpdate(node,value,key){
        node.textContent = value
        new Watcher(this.vm,key,newValue=>{
            node.textContent = newValue
        })
    }
    // v-model
    modelUpdate(node,value,key){
        node.value = value
        new Watcher(this.vm,key,newValue=>{
            node.value = newValue
        })
        node.addEventListener('input',()=>{
            this.vm[key] = node.value
        })
    }
    htmlUpdate(node,value,key) {
        node.innerHTML = value
        new Watcher(this.vm,key,newValue=>{
            node.innerHTML = newValue
        })
    }
    onUpdate(node,value,key,eventName){

        node.addEventListener(eventName,()=>{
            let enentFn = this.vm.$options.methods[key]
            if(enentFn){
                enentFn.call(this)
            }else{
                console.log(key,'s')
                eval(key)
            }
            
        })
    }
    // 编译文本节点，处理插值表达式
    compileText(node){
        let reg = /\{\{(.+?)\}\}/
        let value = node.textContent
        if(reg.test(value)) {
            let key = RegExp.$1.trim()
            node.textContent = value.replace(reg,this.vm[key])
            new Watcher(this.vm,key,newValue=>{
                node.textContent = newValue
            })
        }
    }
    // 判断元素属性是否是指令，也就是是否以v-开头
    isDirective(attrName){
        return attrName.startsWith('v-')||attrName.startsWith('@')
    }

    isTextNode(node){
        // nodeType ==3 文本节点， ==1 元素节点
        return node.nodeType === 3

    }
    isElementNode(node){
        return node.nodeType === 1
    }
}